"""
Case Type   : GUC
Case Name   : 验证设置pre_auth_delay参数是否影响wdr功能
Create At   : 2024/10/14
Owner       : nanyang12
Description :
        1.设置guc参数pre_auth_delay=10,enable_wdr_snapshot=on
        2.建表执行DML操作
        3.创建快照并查询已经生成的快照
        4.清理环境，恢复参数默认值
Expect      :
        1.设置成功
        2.执行成功
        3.创建快照成功，数据库状态正常
        4.默认值恢复成功
History     :
"""
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

LOG = Logger()
commonsh = CommonSH('dbuser')


class DeveloperOptions(unittest.TestCase):
    def setUp(self):
        self.constant = Constant()
        self.user_node = Node('dbuser')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        LOG.info(
            '------Opengauss_Function_DeveloperOptions_Case0116start----')

    def test_post_auth_delay(self):
        LOG.info('--步骤1:设置参数值pre_auth_delay=10,enable_wdr_snapshot=on，并查询数据库状态正常--')
        msg = commonsh.execute_gsguc('reload',
                                     self.constant.GSGUC_SUCCESS_MSG,
                                     'pre_auth_delay=10')
        LOG.info(msg)
        self.assertTrue(msg)
        msg2 = commonsh.execute_gsguc('reload',
                                      self.constant.GSGUC_SUCCESS_MSG,
                                      'enable_wdr_snapshot=on')
        LOG.info(msg2)
        self.assertTrue(msg2)
        status = commonsh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)
        LOG.info('--步骤1.2:查看参数pre_auth_delay修改后的值为10s--')
        sql_cmd = commonsh.execut_db_sql('show pre_auth_delay;')
        LOG.info(sql_cmd)
        self.assertIn('10s', sql_cmd)
        LOG.info('--步骤1.3:查看参数enable_wdr_snapshot修改后的值为on--')
        sql_cmd = commonsh.execut_db_sql('show enable_wdr_snapshot;')
        LOG.info(sql_cmd)
        self.assertIn('on', sql_cmd)

        LOG.info('--步骤2:建表执行DML操作--')
        sql_cmd = '''drop table if exists t_developer_options_0116;
                     create table t_developer_options_0116(id int,name text);
                     insert into t_developer_options_0116 values(2,'jack'),(3,'joe'),(4,'alice');
                     update t_developer_options_0116 set name = 'tom' where id =2;
                     delete from t_developer_options_0116 where id =3;
                     select * from t_developer_options_0116;
                    '''
        excute_cmd1 = f'''source {self.DB_ENV_PATH};
            gsql -d postgres -p {self.user_node.db_port} -c "{sql_cmd}"
            '''
        LOG.info(excute_cmd1)
        msg1 = self.user_node.sh(excute_cmd1).result()
        LOG.info(msg1)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, msg1)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, msg1)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, msg1)
        self.assertIn(self.constant.UPDATE_SUCCESS_MSG, msg1)
        self.assertIn(self.constant.DELETE_SUCCESS_MSG, msg1)
        self.assertIn('4 | alice', msg1)
        self.assertIn('2 | tom', msg1)

        LOG.info('--步骤3:创建快照并查询已经生成的快照--')
        sql_cmd = '''select create_wdr_snapshot();
                     select * from snapshot.snapshot;
                    '''
        excute_cmd1 = f'''source {self.DB_ENV_PATH};
            gsql -d postgres -p {self.user_node.db_port} -c "{sql_cmd}"
            '''
        LOG.info(excute_cmd1)
        msg1 = self.user_node.sh(excute_cmd1).result()
        LOG.info(msg1)
        self.assertIn('WDR snapshot request has been submitted', msg1)
        self.assertIn('0 rows', msg1)
        LOG.info('--查看数据库状态正常--')
        status = commonsh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)

    def tearDown(self):
        LOG.info('--步骤4:恢复默认值--')
        msg = commonsh.execute_gsguc('reload',
                                     self.constant.GSGUC_SUCCESS_MSG,
                                     'pre_auth_delay=0')
        LOG.info(msg)
        msg = commonsh.execute_gsguc('reload',
                                     self.constant.GSGUC_SUCCESS_MSG,
                                     'enable_wdr_snapshot=off')
        LOG.info(msg)
        msg = commonsh.restart_db_cluster()
        LOG.info(msg)
        status = commonsh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)
        LOG.info(
            '------Opengauss_Function_DeveloperOptions_Case0116finish----')
